package com.family.tools;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class EncrypMD5 {

    public static  String  eccrypt(String info){
        //根据MD5算法生成MessageDigest对象
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] srcBytes = info.getBytes();
            //使用srcBytes更新摘要
            md5.update(srcBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //完成哈希计算，得到result
        byte[] resultBytes = md5.digest();
        return bytes2HexString(resultBytes);
    }



    public static String bytes2HexString(byte[] b) {
        String ret = "";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[ i ] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            ret += hex.toUpperCase();
        }
        return ret;
    }


    public static  void HexString2bytes(String inbuf,byte[] outbuf,int len)
    {
        for(int i=0;i<len/2;i++)
        {
            String tmp = inbuf.substring(i*2, i*2+2);
            outbuf[i] = (byte)Integer.parseInt(tmp, 16);
        }
    }

//	public static void main(String args[]) throws NoSuchAlgorithmException{
////		String msg = "郭XX-精品相声技术";
////		EncrypMD5 md5 = new EncrypMD5();
////		byte[] resultBytes = md5.eccrypt(msg);
////
////		String hex = bytes2HexString(resultBytes);
////		System.out.println(hex);
////
////		byte[] outbuf = new byte[2*1024];
////		HexString2bytes(hex,outbuf,hex.length());
////		byte[] send = Arrays.copyOfRange(outbuf,0,hex.length()/2);
////
////		System.out.println(bytes2HexString(send));
//
//
////		System.out.println("密文是：" + new String(resultBytes));
////		System.out.println("明文是：" + msg);
//	}


}
